package bootstrap.liftweb

import _root_.net.liftweb.util._
import _root_.net.liftweb.common._
import _root_.net.liftweb.http._
import _root_.net.liftweb.http.provider._
import _root_.net.liftweb.sitemap._
import _root_.net.liftweb.sitemap.Loc._
import pl.edu.romanow.dispatch.FacebookDispatch


/**
 * A class that's instantiated early and run.  It allows the application
 * to modify lift's environment
 */
class Boot extends Loggable {
  def boot {

    // where to search snippet
    LiftRules.addToPackages("pl.edu.romanow")

    // Build SiteMap
    def sitemap() = SiteMap(
      (Menu(Loc("Static", Link(List("static"), true, "/static/index"), "Static Content")) ::
        Menu(Loc("popupRefresh", "util" :: "popupRefresh" :: Nil, S.?("util.popupRefresh"), Loc.Hidden)) ::
        Menu(Loc("popupRefresh", "popupRefresh" :: "util" :: Nil, S.?("util.popupRefresh"), Loc.Hidden)) ::
        Menu(Loc("graph", "graph" :: Nil, S.?("graph"))) ::
        Nil): _*)

    LiftRules.dispatch.append(FacebookDispatch.dispatch)

    /*
     * Show the spinny image when an Ajax call starts
     */
    LiftRules.ajaxStart =
      Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd)

    /*
     * Make the spinny image go away when it ends
     */
    LiftRules.ajaxEnd =
      Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd)

    LiftRules.early.append(makeUtf8)

  }

  /**
   * Force the request to be UTF-8
   */
  private def makeUtf8(req: HTTPRequest) {
    req.setCharacterEncoding("UTF-8")
  }
}
